package com.tanhua.server.controller;

import com.tanhua.server.service.VideoService;
import com.tanhua.server.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("smallVideos")
public class VideoController {

    @Autowired
    private VideoService videoService;

    /**
     * 发布小视频
     *
     * @param picFile
     * @param videoFile
     * @return
     */
    @PostMapping
    public Object saveVideo(@RequestParam("videoThumbnail") MultipartFile picFile,
                            @RequestParam("videoFile") MultipartFile videoFile) {
        return this.videoService.saveVideo(picFile, videoFile);
    }

    /**
     * 查询小视频列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public PageResult queryVideoList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return this.videoService.queryVideoList(Math.max(page, 1), pageSize);
    }

    /**
     * 视频点赞
     *
     * @param videoId 视频id
     * @return
     */
    @PostMapping("/{id}/like")
    public Long likeComment(@PathVariable("id") String videoId) {

        return this.videoService.likeComment(videoId);
    }

    /**
     * 取消点赞
     *
     * @param videoId
     * @return
     */
    @PostMapping("/{id}/dislike")
    public Long disLikeComment(@PathVariable("id") String videoId) {

        return this.videoService.disLikeComment(videoId);
    }

    /**
     * 查询评论列表
     *
     * @param videoId
     * @return
     */
    @GetMapping("/{id}/comments")
    public Object queryCommentList(@PathVariable("id") String videoId,
                                   @RequestParam(value = "page", defaultValue = "1") Integer page,
                                   @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        PageResult pageResult = this.videoService.queryCommentList(videoId, page, pageSize);
        if (pageResult != null) {
            return ResponseEntity.ok(pageResult);
        }
        return null;
    }

    /**
     * 评论发布
     *
     * @param publishId 目标id
     * @param comment   评论内容
     * @return
     */
    @PostMapping("/{id}/comments")
    public Object publishComment(@PathVariable("id") String publishId,
                                 @RequestBody Map<String, String> comment) {

        return this.videoService.publishComment(publishId, comment);

    }

    /**
     * 评论点赞
     * @param publishId 评论的id
     * @return
     */
    @PostMapping("/comments/{id}/like")
    public Object likeComments(@PathVariable("id")String publishId){
        return this.videoService.likeComment(publishId);


    }

    /**
     * 取消点赞
     * @param publishId 评论的id
     * @return
     */
    @PostMapping("/comments/{id}/dislike")
    public Object disLikeComments(@PathVariable("id")String publishId){
        return this.videoService.disLikeComment(publishId);

    }

    /**
     * 视频 关注
     * @param uid
     * @return
     */
    @PostMapping("/{uid}/userFocus")
    public Object userFous(@PathVariable("uid")String uid){
        return this.videoService.userFocus(uid);

    }

    /**
     * 取消关注
     * @param uid
     * @return
     */
    @PostMapping("/{uid}/userUnFocus")
    public Object userUnFocus(@PathVariable("uid")String uid){
        return this.videoService.userUnFocus(uid);
    }
}
